using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;

namespace Presentation.Resources
{
    public partial class UserControlHoadonban : UserControl
    {
        private int KhachHangMoi = 0;
        public static HoaDonBanDto obj;
        public static TienTrongNgayDto ttn;
        public UserControlHoadonban()
        {
            InitializeComponent();
        }


        private void chkLieu_Click(object sender, EventArgs e)
        {
            if (KhachHangMoi == 1)
            {
                //Tao mot hoa don moi
                obj = new HoaDonBanDto();
                obj.MaHoaDon = "";
                obj.MaNhanVien = FrmDangNhap.nv.Id;
                obj.NgayTao = DateTime.Parse(txtNgayTao.Text);
                obj.TongTien = 0;
                obj.Vat = 0;
                obj.SoTienThanhToan = 0;
                obj.LoaiHd = 0;
                //luu xuong csdl
                HoaDonBanDao h = new HoaDonBanDao();
                h.SaveOrUpdate(obj);
                KhachHangMoi = 0;
            }
            chkLieu.Checked = true;
            chkMon.Checked = false;
            chkToa.Checked = false;
            FrmLieu f = new FrmLieu();
            f.Show();
        }

        private void chkMon_Click(object sender, EventArgs e)
        {
            if (KhachHangMoi == 1)
            {
                //Tao mot hoa don moi
                obj = new HoaDonBanDto();
                obj.MaHoaDon = "";
                obj.MaNhanVien = FrmDangNhap.nv.Id;
                obj.NgayTao = DateTime.Parse(txtNgayTao.Text);
                obj.TongTien = 0;
                obj.Vat = 0;
                obj.SoTienThanhToan = 0;
                obj.LoaiHd = 0;
                //luu xuong csdl
                HoaDonBanDao h = new HoaDonBanDao();
                h.SaveOrUpdate(obj);
                KhachHangMoi = 0;
            }
            chkMon.Checked = true;
            chkToa.Checked = false;
            chkLieu.Checked = false;
            FrmMon f = new FrmMon();
            f.Show();
        }

        private void chkToa_Click(object sender, EventArgs e)
        {
            if (KhachHangMoi == 1)
            {
                //Tao mot hoa don moi
                obj = new HoaDonBanDto();
                obj.MaHoaDon = "";
                obj.MaNhanVien = FrmDangNhap.nv.Id;
                obj.NgayTao = DateTime.Parse(txtNgayTao.Text);
                obj.TongTien = 0;
                obj.Vat = 0;
                obj.SoTienThanhToan = 0;
                obj.LoaiHd = 0;
                //luu xuong csdl
                HoaDonBanDao h = new HoaDonBanDao();
                h.SaveOrUpdate(obj);
                KhachHangMoi = 0;
            }
            chkToa.Checked = true;
            chkLieu.Checked = false;
            chkMon.Checked = false;
            frmToa f = new frmToa();
            f.Show();
        }

        private void UserControlHoadonban_Load(object sender, EventArgs e)
        {
            NhanVienDto nv = FrmDangNhap.nv;
            txtNhanvien.Text = nv.TenNhanVien;
            txtNgayTao.Text = DateTime.Now.ToShortDateString();
            KhachHangMoi = 1;
            txtTongtien.Text = "0";

            //Tim kiem doi tuong tien trong ngay tuong ung voi ngay hien hanh
            ttn = new TienTrongNgayDto();
            ttn.Id = 0;
            ttn.NgayBan = DateTime.Parse(txtNgayTao.Text);
            ttn.TongTien = 0;
            ttn = TienTrongNgayDao.TimKiemTheoNgay(ttn);
            if (ttn.Id == 0)
                ttn = TienTrongNgayDao.ThemTienTrongNgay(ttn);

        }

        private void btnXuat_Click(object sender, EventArgs e)
        {
            try
            {
                //Cap nhat lai tien trong ngay
                ttn.TongTien += int.Parse(txtTongtien.Text);
                TienTrongNgayDao.CapNhatTienTrongNgay(ttn);
                //

                //Reset de tao hoa don moi
                KhachHangMoi = 1;
                obj = new HoaDonBanDto();
                UserControlHoadonban.dgrDanhsachcachangban.RowCount = 1;
                UserControlHoadonban.txtTongtien.Text = 0.ToString();
                //
                chkLieu.Checked = false;
                chkMon.Checked = false;
                chkToa.Checked = false;
                //code cho chuc nang in hoa don o phia duoi dong nay

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
